#include <REGX52.H>
#include "LCD1602.h"
#include "DS18B02.h"
#include "Delay.h"
#include "AT24C02.h"
#include "Key.h"
#include "Timer0.h"


float T,TShow;
char TLow,THigh;
unsigned char KeyNum;

void main()
{
	DS18B20_ConvertT();				//上电先转换一次温度，防止第一次读数据错误
	Delay(1000);					//等待转换完成
	THigh=AT24C02_ReadByte(0);	//读取温度阈值数据
	TLow=AT24C02_ReadByte(1);
	if(THigh>125 || TLow<-55 || THigh<=TLow)
	{
		THigh=35;			//如果阈值非法，则设为默认值
		TLow=28;
	}
	LCD_Init();
	LCD_ShowString(1,1,"T:");
	LCD_ShowString(2,1,"TH:");
	LCD_ShowString(2,9,"TL:");
	LCD_ShowSignedNum(2,4,THigh,3);
	LCD_ShowSignedNum(2,12,TLow,3);
	Timer0_Init();
	while(1)
	{
		KeyNum = Key();
		
		/*温度读取及显示*/
		DS18B20_ConvertT();
		T = DS18B20_ReadT();
		if(T<0)
		{
			LCD_ShowChar(1,3,'-');	//显示负号
			TShow=-T;					//将温度变为正数
		}
		else
		{
			LCD_ShowChar(1,3,'+');	//显示负号
			TShow = T;
		}
		LCD_ShowNum(1,4,TShow,3);		//显示温度整数部分
		LCD_ShowChar(1,7,'.');		//显示小数点
		LCD_ShowNum(1,8,(unsigned long)(TShow*10000)%10000,4);//显示温度小数部分
		//先乘以 10000，把小数变整数，int放不下了，用long放，然后%10000，就是四位小数
		
		/*阈值判断及显示*/
		if(KeyNum)
		{
			if(KeyNum == 1)
			{
				THigh++;
				if(THigh>125){THigh=125;}
			}
			if(KeyNum == 2)
			{
				THigh--;
				if(THigh<=TLow){THigh++;}
			}
			if(KeyNum == 3)
			{
				TLow++;
				if(TLow>=THigh){TLow--;}
			}
			if(KeyNum == 4)
			{
				TLow--;
				if(TLow<-55){TLow=-55;}
			}
			LCD_ShowSignedNum(2,4,THigh,3);
			LCD_ShowSignedNum(2,12,TLow,3);
			AT24C02_WriteByte(0,THigh);		//写入到At24C02中保存
			Delay(5);
			AT24C02_WriteByte(1,TLow);
			Delay(5);
		}
		if(T>THigh)			//越界判断
		{
			LCD_ShowString(1,13,"OV:H");
		}
		else if(T<TLow)
		{
			LCD_ShowString(1,13,"OV:L");
		}
		else
		{
			LCD_ShowString(1,13,"    ");
		}
	}
}

void Timer0_Rountine() interrupt 1 //这个是中断号，就是相当于是你T0发生中断了，就会跳到这个地方来，interrupt 1就是T0的中断号，前面的名字是自己取的
{
	static unsigned int Timer0_count=0;
	TH0=64535/256;//重新初始化
	TL0=64535%256;
	Timer0_count++;
	if(Timer0_count>=20)//1s
	{
		Timer0_count=0;
		Key_Loop();
	}
}